package com.example.myapplication;

import android.app.Activity;
import android.content.Context;
import android.media.AudioPresentation;
import android.media.tv.AitInfo;
import android.media.tv.TvContentRating;
import android.media.tv.TvContract;
import android.media.tv.TvInputInfo;
import android.media.tv.TvInputManager;
import android.media.tv.TvTrackInfo;
import android.media.tv.TvView;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;

import java.util.List;


public class MainActivity extends Activity {
    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TvInputManager manager = (TvInputManager) getSystemService(Context.TV_INPUT_SERVICE);
        List<TvInputInfo> tvInputList = manager.getTvInputList();
        Log.i(TAG, "onCreate: tvInputList " + tvInputList.size());

        FrameLayout frameLayout = new FrameLayout(this);
        frameLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        TvView tvView = new TvView(this);
        ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(1920 , 1080);
        layoutParams.dimensionRatio = "16:9";
        tvView.setLayoutParams(layoutParams);

        frameLayout.addView(tvView);

        setContentView(frameLayout);


        manager.registerCallback(new TvInputManager.TvInputCallback() {
            @Override
            public void onInputStateChanged(String inputId, int state) {
                Log.i(TAG, "onInputStateChanged: " + inputId);
                super.onInputStateChanged(inputId, state);
                if (state == 0) {
                    tvView.tune(inputId, TvContract.buildChannelUriForPassthroughInput(inputId));
                }
            }

            @Override
            public void onInputAdded(String inputId) {
                super.onInputAdded(inputId);
                Log.i(TAG, "onInputAdded: " + inputId);
            }

            @Override
            public void onInputRemoved(String inputId) {
                super.onInputRemoved(inputId);
                Log.i(TAG, "onInputRemoved: " + inputId);
            }

            @Override
            public void onInputUpdated(String inputId) {
                super.onInputUpdated(inputId);
                Log.i(TAG, "onInputUpdated: " + inputId);
            }

            @Override
            public void onTvInputInfoUpdated(TvInputInfo inputInfo) {
                super.onTvInputInfoUpdated(inputInfo);
                Log.i(TAG, "onTvInputInfoUpdated: ");
            }
        }, new Handler());


        tvView.setCallback(new TvView.TvInputCallback() {
            @Override
            public void onConnectionFailed(String inputId) {
                super.onConnectionFailed(inputId);
                Log.d(TAG, "onConnectionFailed: " + inputId);
            }

            @Override
            public void onDisconnected(String inputId) {
                super.onDisconnected(inputId);
                Log.d(TAG, "onDisconnected: " + inputId);
            }

            @Override
            public void onChannelRetuned(String inputId, Uri channelUri) {
                super.onChannelRetuned(inputId, channelUri);
                Log.d(TAG, "onChannelRetuned: inputId: " + inputId + "channelUri: " + channelUri);
            }

            @Override
            public void onAudioPresentationsChanged(@NonNull String inputId, @NonNull List<AudioPresentation> audioPresentations) {
                super.onAudioPresentationsChanged(inputId, audioPresentations);
                Log.d(TAG, "onAudioPresentationsChanged: " + inputId);
            }

            @Override
            public void onAudioPresentationSelected(@NonNull String inputId, int presentationId, int programId) {
                super.onAudioPresentationSelected(inputId, presentationId, programId);
            }

            @Override
            public void onTracksChanged(String inputId, List<TvTrackInfo> tracks) {
                super.onTracksChanged(inputId, tracks);
                Log.d(TAG, "onTracksChanged: " + inputId);
            }

            @Override
            public void onTrackSelected(String inputId, int type, String trackId) {
                super.onTrackSelected(inputId, type, trackId);
                Log.d(TAG, "onTrackSelected: " + inputId);
            }

            @Override
            public void onVideoSizeChanged(String inputId, int width, int height) {
                super.onVideoSizeChanged(inputId, width, height);
                Log.d(TAG, "onVideoSizeChanged: " + inputId);
            }

            @Override
            public void onVideoAvailable(String inputId) {
                super.onVideoAvailable(inputId);
                Log.d(TAG, "onVideoAvailable: " + inputId);
            }

            @Override
            public void onVideoUnavailable(String inputId, int reason) {
                super.onVideoUnavailable(inputId, reason);
                Log.d(TAG, "onVideoUnavailable inputId: " + inputId);
                Log.d(TAG, "onVideoUnavailable reason: " + reason);
            }

            @Override
            public void onContentAllowed(String inputId) {
                super.onContentAllowed(inputId);
                Log.d(TAG, "onContentAllowed: " + inputId);
            }

            @Override
            public void onContentBlocked(String inputId, TvContentRating rating) {
                super.onContentBlocked(inputId, rating);
                Log.d(TAG, "onContentBlocked: " + inputId);
            }

            @Override
            public void onTimeShiftStatusChanged(String inputId, int status) {
                super.onTimeShiftStatusChanged(inputId, status);
                Log.d(TAG, "onTimeShiftStatusChanged: " + inputId);
            }

            @Override
            public void onAitInfoUpdated(@NonNull String inputId, @NonNull AitInfo aitInfo) {
                super.onAitInfoUpdated(inputId, aitInfo);
                Log.d(TAG, "onAitInfoUpdated: " + inputId);
            }

            @Override
            public void onSignalStrengthUpdated(@NonNull String inputId, int strength) {
                super.onSignalStrengthUpdated(inputId, strength);
                Log.d(TAG, "onSignalStrengthUpdated: " + inputId);
            }

            @Override
            public void onCueingMessageAvailability(@NonNull String inputId, boolean available) {
                super.onCueingMessageAvailability(inputId, available);
                Log.d(TAG, "onCueingMessageAvailability: " + inputId);
            }

            @Override
            public void onTimeShiftMode(@NonNull String inputId, int mode) {
                super.onTimeShiftMode(inputId, mode);
                Log.d(TAG, "onTimeShiftMode: " + inputId);
            }

            @Override
            public void onAvailableSpeeds(@NonNull String inputId, @NonNull float[] speeds) {
                super.onAvailableSpeeds(inputId, speeds);
                Log.d(TAG, "onAvailableSpeeds: " + inputId);
            }

            @Override
            public void onTuned(@NonNull String inputId, @NonNull Uri channelUri) {
                super.onTuned(inputId, channelUri);
                Log.d(TAG, "onTuned: " + inputId);
            }

            @Override
            public void onTvMessage(@NonNull String inputId, int type, @NonNull Bundle data) {
                super.onTvMessage(inputId, type, data);
                Log.d(TAG, "onTvMessage inputId: " + inputId);
                Log.d(TAG, "onTvMessage type: " + type);
                Log.d(TAG, "onTvMessage data: " + data);
            }
        });

        for (TvInputInfo tvInputInfo : tvInputList) {
            Log.i(TAG, "onCreate: " + tvInputInfo.getId());
            Log.i(TAG, "onCreate: " + tvInputInfo.getType());

            if (TvInputInfo.TYPE_HDMI == tvInputInfo.getType()) {
                String id = tvInputInfo.getId();
                tvView.tune(id, TvContract.buildChannelUriForPassthroughInput(id));
                tvView.setActivated(true);
                break;
            }
        }
    }
}